package com.ruoyi.dl.controller;

import java.util.List;

import com.ruoyi.dl.domain.DlOrdersGoods;
import com.ruoyi.dl.domain.DlUser;
import com.ruoyi.dl.service.IDlOrdersGoodsService;
import com.ruoyi.dl.service.IDlUserService;
import com.ruoyi.dl.uitls.Tool;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dl.domain.DlOrders;
import com.ruoyi.dl.service.IDlOrdersService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单Controller
 *
 * @author ruoyi
 * @date 2020-12-28
 */
@Controller
@RequestMapping("/dl/orders")
public class DlOrdersController extends BaseController {
    private String prefix = "dl/orders";

    @Autowired
    private IDlOrdersService dlOrdersService;
    @Autowired
    private IDlUserService dlUserService;
    @Autowired
    private IDlOrdersGoodsService dlOrdersGoodsService;

    @RequiresPermissions("dl:orders:view")
    @GetMapping()
    public String orders() {
        return prefix + "/orders";
    }

    /**
     * 查询订单列表
     */
    @RequiresPermissions("dl:orders:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DlOrders dlOrders) {
        startPage();
        List<DlOrders> list = dlOrdersService.mySelectDlOrdersList(dlOrders);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("dl:orders:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DlOrders dlOrders) {
        List<DlOrders> list = dlOrdersService.selectDlOrdersList(dlOrders);
        ExcelUtil<DlOrders> util = new ExcelUtil<DlOrders>(DlOrders.class);
        return util.exportExcel(list, "orders");
    }

    /**
     * 新增订单
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        List<DlUser> userList = dlUserService.selectDlUserList(new DlUser());
        List<DlOrdersGoods> ordersGoodsList = dlOrdersGoodsService.selectDlOrdersGoodsList(new DlOrdersGoods());
        if (userList != null) {
            modelMap.addAttribute("userList", userList);
        }
        if (ordersGoodsList != null) {
            modelMap.addAttribute("ordersGoodsList", ordersGoodsList);
        }
        return prefix + "/add";
    }

    /**
     * 新增保存订单
     */
    @RequiresPermissions("dl:orders:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DlOrders dlOrders) {
        dlOrders.setOrdersNumber(Tool.generateUniqueKey("DL"));
        return toAjax(dlOrdersService.insertDlOrders(dlOrders));
    }

    /**
     * 修改订单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        List<DlUser> userList = dlUserService.selectDlUserList(new DlUser());
        List<DlOrdersGoods> ordersGoodsList = dlOrdersGoodsService.selectDlOrdersGoodsList(new DlOrdersGoods());
        if (userList != null) {
            mmap.addAttribute("userList", userList);
        }
        if (ordersGoodsList != null) {
            mmap.addAttribute("ordersGoodsList", ordersGoodsList);
        }
        DlOrders dlOrders = dlOrdersService.selectDlOrdersById(id);
        mmap.put("dlOrders", dlOrders);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单
     */
    @RequiresPermissions("dl:orders:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DlOrders dlOrders) {
        return toAjax(dlOrdersService.updateDlOrders(dlOrders));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("dl:orders:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(dlOrdersService.deleteDlOrdersByIds(ids));
    }
}
